package org.ResearchProject.controller;

import org.ResearchProject.dto.Result;
import org.ResearchProject.dto.SendMassageDTO;
import org.ResearchProject.pojo.ResearchTask;
import org.ResearchProject.pojo.Student;
import org.ResearchProject.service.ResearchTaskService;
import org.ResearchProject.service.StudentService;
import org.ResearchProject.utils.SendMassageUtil;
import org.ResearchProject.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 科研任务管理
 */

@RestController
@RequestMapping("/task")
public class ResearchTaskController {
    @Autowired
    ResearchTaskService researchTaskService;
    @Autowired
    StudentService studentService;
    @Autowired
    SendMassageUtil sendMassageUtil;
    /**
     * 添加科研任务
     */
    @RequestMapping(value = "/addProjectTask",method = RequestMethod.POST)
    public Result addTask(@RequestBody ResearchTask researchTask){
        Map<String,Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        System.out.println(researchTask);
        researchTaskService.addTask(researchTask);
        return Result.success("添加科研任务成功！");
    }
    /**
     * 删除科研任务
     */
    @RequestMapping(value = "/deleteProjectTask",method = RequestMethod.POST)
    public Result deleteTask(Integer taskId){
        Map<String,Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        researchTaskService.deleteTask(taskId);
        return Result.success("删除科研任务成功！");
    }
    /**
     * 更新科研任务
     */
    @RequestMapping(value = "/updateProjectTask",method = RequestMethod.PUT)
    public Result updateTask(@RequestBody ResearchTask researchTask){
        Map<String,Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        researchTaskService.updateTask(researchTask);
        return Result.success("更新科研任务成功！");
    }
    /**
     * 通过id查询科研任务
     */
    @RequestMapping(value = "/selectTaskById",method = RequestMethod.GET)
    public Result selectTaskById(Integer taskId){
        return Result.success(researchTaskService.selectTaskById(taskId));
    }
    /**
     * 查询自己的所有科研任务
     */
    @RequestMapping(value = "/selectAllTask",method = RequestMethod.GET)
    public Result selectAllTask(){
        Map<String,Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")==1){
            // 学生
            return Result.success(researchTaskService.StudentSelectAllTask((Integer) msg.get("id")));
        }
        if((int)msg.get("auth")==0){
            // 老师
            return Result.success(researchTaskService.TutorSelectAllTask((Integer) msg.get("id")));
        }
        return Result.error("权限错误！");
    }
    /**
     * 给学生指派科研任务
     */
    @RequestMapping(value = "/TaskToStudent",method = RequestMethod.POST)
    public Result publishTask(@RequestParam Integer taskId,
                              @RequestParam Integer studentId,
                              @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                              @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) throws MessagingException {
        Map<String,Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        researchTaskService.giveTaskToStudent(taskId,studentId,startTime,endTime,(Integer) msg.get("id"));
        // 发邮件提醒学生
        Student student = studentService.selectByUseId(studentId);
        SendMassageDTO sendMassageDTO = new SendMassageDTO();
        sendMassageDTO.setSubject("科研任务提醒");
        sendMassageDTO.setContent("你的科研任务："+researchTaskService.selectTaskById(taskId).get("taskName")+"已发布，请及时完成！");
        sendMassageDTO.setFrom("1306134542@qq.com");
        sendMassageDTO.setTo(student.getEmail());
        sendMassageUtil.sendMsg(sendMassageDTO);
        return Result.success("指派成功！");
    }
    /**
     * 学生提交科研任务
     */
    @RequestMapping(value = "/submitTask",method = RequestMethod.POST)
    public Result submitTask(@RequestParam Integer taskId, @RequestParam String text){
        Map<String,Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=1)
            return Result.error("没有权限！");
        researchTaskService.submitTask(taskId,text,(Integer) msg.get("id"));
        return Result.success("提交成功！");
    }
}
